//
//  ZJTabbar.swift
//  Webo
//
//  Created by 曾静 on 16/6/25.
//  Copyright © 2016年 qq. All rights reserved.
//

import UIKit

class ZJTabbar: UITabBar {

    var modelClourse:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setBarButton()
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setBarButton(){
        
        addSubview(composeBtn)
        
        
        
    }
    
    
    //创建控件
    private lazy var composeBtn: UIButton = {
        
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.addTarget(self, action: "addBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        btn .sizeToFit()
        
        return btn
    }()
    
    
   @objc private func addBtnClick(){
    
      
    
        modelClourse?()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        composeBtn.center.x = frame.size.width * 0.5
        composeBtn.center.y = frame.size.height * 0.5
        
        //y是相对于父控件的有值xxx
        //comoseBtn.center = self.center
        
        let width:CGFloat = UIScreen.mainScreen().bounds.width * 0.2
        
//        let height:CGFloat = bounds.height
        
        var index:CGFloat = 0
        
        
        for subBtn in subviews{
            
            if subBtn.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
             subBtn.frame.origin.x = width * index
             subBtn.frame.size.width = width
            
                
                if index == 1 {
                    index++
                }
                
                index++
            
            }
        }
        
        
        
    }

}
